package q2016_03_01.behaviour;

/**
 * ״̬ģʽ
 * @author ZHANGHJ14
 *
 */
public class StatePattern {

	public static void main(String[] args) {
		Context context = new Context(new ConcreteState1());
		context.request();
		context.request();
		context.request();
		context.request();
	}

}

class Context{
	private State state;
	
	public Context(State state) {
		super();
		this.state = state;
	}
	
	public void request(){
		state.handle(this);
	}

	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}
}

interface State{
	void handle(Context context);
}

class ConcreteState1 implements State{
	@Override
	public void handle(Context context) {
		System.out.println("ConcreteState1 handle");
		context.setState(new ConcreteState2());
	}
}

class ConcreteState2 implements State{
	@Override
	public void handle(Context context) {
		System.out.println("ConcreteState2 handle");
		context.setState(new ConcreteState1());
	}
}